﻿using System;
using Common.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Common.Logging.Loupe.Test
{
    [TestClass]
    public class LoupeFactoryTest
    {
        [TestMethod]
        public void CreateLogger()
        {
            LoupeFactoryAdapter adapter = new LoupeFactoryAdapter();
            var logger = adapter.GetLogger("Test");

            Assert.IsNotNull(logger, "Failed to retrieve a logger from the factory");
        }

        [TestMethod]
        public void LoggerNameAndTypeEquality()
        {
            LoupeFactoryAdapter adapter = new LoupeFactoryAdapter();
            var logger1 = adapter.GetLogger(this.GetType());
            var logger2 = adapter.GetLogger(this.GetType().FullName);

            // Intended reference comparison - we are caching loggers
            Assert.AreEqual(logger1, logger2);
        }

        [TestMethod]
        public void LogLevelSet()
        {
            LoupeFactoryAdapter adapter = new LoupeFactoryAdapter();

            adapter.CurrentLevel = LogLevel.Debug;

            Assert.AreEqual(adapter.CurrentLevel, LogLevel.Debug, "Setting active log level did not succeed.");

            var logger1 = adapter.GetLogger(this.GetType()) as LoupeLogger;

            Assert.IsNotNull(logger1, "Adapter did not return the expected type.");

            Assert.AreEqual(logger1.CurrentLevel, LogLevel.Debug, "Logger created with incorrect value");            
        }

        [TestMethod]
        public void CachedLogLevelSet()
        {
            LoupeFactoryAdapter adapter = new LoupeFactoryAdapter();

            adapter.CurrentLevel = LogLevel.Debug;

            Assert.AreEqual(adapter.CurrentLevel, LogLevel.Debug, "Setting active log level did not succeed.");

            var logger1 = adapter.GetLogger(this.GetType()) as LoupeLogger;

            Assert.IsNotNull(logger1, "Adapter did not return the expected type.");

            Assert.AreEqual(logger1.CurrentLevel, LogLevel.Debug, "Logger created with incorrect value.");

            adapter.CurrentLevel = LogLevel.Info;

            Assert.AreEqual(logger1.CurrentLevel, LogLevel.Info, "Cached adapter active level was not changed.");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void NullCreate()
        {
            LoupeFactoryAdapter adapter = new LoupeFactoryAdapter();
            var logger = adapter.GetLogger((Type) null);

            Assert.Fail("Null create did not throw");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void EmptyCreate()
        {
            LoupeFactoryAdapter adapter = new LoupeFactoryAdapter();
            var logger = adapter.GetLogger(string.Empty);

            Assert.Fail("String.Empty create did not throw");
        }
    }
}
